﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityTypes;
using System.Xml;

namespace SI2_TrabalhoFinal
{
    public partial class RelatorioFinanceiro : Form
    {
        SI2FinalEntities entities = Connection.entities;
        String modo;

        public RelatorioFinanceiro()
        {
            InitializeComponent();
        }

        public RelatorioFinanceiro(string p)
        {
            InitializeComponent();
            modo=p;
            this.button1.Text = p;
            this.dateTimePicker1.Format=DateTimePickerFormat.Custom;
            this.dateTimePicker1.CustomFormat="MMMM yyyy";           
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value.Year > DateTime.Now.Year && (dateTimePicker1.Value.Month >= DateTime.Now.Month && dateTimePicker1.Value.Year == DateTime.Now.Year))
            {
                MessageBox.Show("Data Inválida");
            }
            
            var query = from date in entities.RelatorioMensalFinanceiro
                        select date;
            foreach (var v in query)
            {
                if (dateTimePicker1.Value.Month==v.Mes && dateTimePicker1.Value.Year==v.Ano)
                {
                    if (modo == "Produzir")
                    {
                        MessageBox.Show("Relatório já produzido!");
                        return;
                    }
                    if (modo == "Salvar")
                    {
                        SaveFileDialog saveDialog = new SaveFileDialog();
                        saveDialog.Filter = "CSV file|*.csv";
                        saveDialog.Title = "Salvar ficheiro";
                        
                        saveDialog.FileOk += new System.ComponentModel.CancelEventHandler( (bsender,be) => {
                            if (saveDialog.FileName != "")
                            {
                                System.IO.FileStream fs =
                                (System.IO.FileStream)saveDialog.OpenFile();
                                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                                XmlDocument xs = new XmlDocument();
                                xs.LoadXml(v.Relatorio);
                                XmlNode root = xs.DocumentElement;
                                foreach (XmlNode node in root)
                                {
                                    foreach (XmlNode subnode in node)
                                    {
                                        sw.WriteLine(subnode.Name + "," + subnode.InnerText);
                                    }
                                }
                                sw.Flush();
                                fs.Flush();
                                sw.Close();
                                fs.Close();
                            }
                        });
                        saveDialog.ShowDialog();
                        return;
                    }
                }
            }
            if (modo == "Salvar")
            {
                MessageBox.Show("Relatório Inexistente!");
                return;
            }
            if (modo == "Produzir")
            {
                entities.RelatorioFinanceiro(dateTimePicker1.Value.Month, dateTimePicker1.Value.Year);
                return;
            }                        
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        

        

        
    }
}
